#include "AssistantTool.h"

CSuccessRateCalc & CSuccessRateCalc::operator =( CSuccessRateCalc & src )
{
	m_lstSuccessRecords  = src.m_lstSuccessRecords;
	m_SumRequest = src.m_SumRequest;
	m_SumSuccess = src.m_SumSuccess;
	m_HistoryRequst = src.m_HistoryRequst;
	m_HistorySucc = src.m_HistorySucc;
	m_CalcWin = src.m_CalcWin;
	return *this;
}

void CSuccessRateCalc::RecordResult(Bool isSuccess)
{
	m_mtxSuccessRecords.Lock();
	m_SumRequest++;
	m_HistoryRequst++;
	if(isSuccess)
	{
		m_SumSuccess++;
		m_HistorySucc++;
	}

	m_lstSuccessRecords.push_back(isSuccess);
	while(m_lstSuccessRecords.size() > m_CalcWin)
	{
		m_SumRequest--;
		if(m_lstSuccessRecords.front())
		{
			m_SumSuccess--;
		}
		m_lstSuccessRecords.pop_front();
	}
	m_mtxSuccessRecords.Unlock();
}

UInt32 CSuccessRateCalc::GetSuccessRate()
{
	m_mtxSuccessRecords.Lock();
	while(m_lstSuccessRecords.size() > m_CalcWin)
	{
		m_SumRequest--;
		if(m_lstSuccessRecords.front())
		{
			m_SumSuccess--;
		}
		m_lstSuccessRecords.pop_front();
	}

	UInt32 nTmp = (m_SumRequest != 0) ? (m_SumSuccess * 100 / m_SumRequest) : 100;
	m_mtxSuccessRecords.Unlock();

	return nTmp;
}


